<?php
namespace app\store\model;
use app\common\model\WxappReg as WxappRegModel;
use think\Db;

/**
 * 快速注册小程序模型
 */
class WxappReg extends WxappRegModel
{
    /**
     * 添加
     */
    public function add(array $data,$pay,$store_user_id)
    {
		// 开启事务
        Db::startTrans();
        try {
			if($pay>0){
				//如果需要扣费
				$store = StoreUser::detail($store_user_id);
				if($store['wallet']<$pay){
					$this->error = '账户余额不足，请充值';
					return false;
				}
				$store->wallet = ['dec',$pay];
				$store->save();
				$order = new WebOrder;
				$order->save([
					'order_no' => orderNo(),
					'order_type' => 20,	//扣费
					'pay_price' => $pay,
					'pay_time' => time(),
					'pay_status' => 20,	
					'purpose' => '小程序注册',
					'affair_id' => self::$wxapp_id,
					
				]);
			}
			$data['status'] = 10; //待审核
			$data['wxapp_id'] = self::$wxapp_id;
			$this->allowField(true)->save($data);
			Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
        }
        return false;
    }
	
	/**
     * 编辑
     */
    public function edit(array $data)
    {
		$data['status'] = 10; //待审核
        return $this->allowField(true)->save($data);
    }

}
